package _2_leetcode_21_40._21合并两个有序链表;

/**
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 *
 * @描述
 * @参数
 * @返回值
 * @创建人 mazexal
 * @创建时间 2021/1/9
 */
public class Solution {

    // 递归 + 哨兵头结点
    public static void main(String[] args) {
        ListNode t1 = mergeTwoLists(new ListNode(1, new ListNode(2, new ListNode(4))), new ListNode(1, new ListNode(3, new ListNode(4))));
        ListNode t2 = mergeTwoLists(null,null);
        ListNode t3 = mergeTwoLists(null,new ListNode(0));
        System.out.println("a");
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode org = new ListNode(0);
        getNext(l1, l2, org);
        return org.next;
    }

    public static void getNext(ListNode l1, ListNode l2, ListNode prev) {
        int node1 = 99;
        int node2 = 99;
        if (l1 != null) {
            node1 = l1.val;
        }
        if (l2 != null) {
            node2 = l2.val;
        }
        if (l1 == null && l2 == null) {
            return;
        }
        if (node1 < node2) {
            prev.next = l1;
            getNext(l1.next, l2, l1);
        } else {
            prev.next = l2;
            getNext(l1, l2.next, l2);
        }
    }
}
